package com.central.apps.controller;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.constant.AppConstant;
import com.central.app.service.HandlerService;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import com.central.common.web.PageResult;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/flow")
public class FlowController {
    @Autowired
    private HandlerService handlerService;

    @PostMapping("/initiate")
    public PageResult<Map<String, Object>> initiate(@RequestBody Map<String,Object> params) throws Exception {
        //获取当前登陆人
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        String sql = "select * from (SELECT t.id, t.app, app.flowname as appDesc, t.object, t.biz_id AS bizId, t.proc_def_id AS procDefId, t.proc_ins_id AS procInsId,\n" +
                " t.sub_proc_ins_id AS subProcInsId, t.STATUS, t.createuser, t.updateuser, t.createtime, t.updatetime,\n" +
                " t.remarks, t.biz_num AS bizNum, t.biz_desc AS bizDesc, t.biz_status AS bizStatus " +
                "FROM biz_flow_app t left join biz_app app on t.app = app.code \n" +
                "WHERE t.proc_ins_id IN (select distinct PROC_INST_ID_ from act_ru_actinst where ACT_NAME_ = '发起人' and ASSIGNEE_ = '" + loginUser.getUsername() + "')) t";

        int page = 1;
        int limit = 10;
        if (MapUtils.getInteger(params, AppConstant.BIZ_QUERY_PAGE) != null && MapUtils.getInteger(params, AppConstant.BIZ_QUERY_LIMIT) != null) {
            page = MapUtil.getInt(params, AppConstant.BIZ_QUERY_PAGE);
            limit = MapUtil.getInt(params, AppConstant.BIZ_QUERY_LIMIT);
        }
        return handlerService.pageList(page, limit, sql);
    }
}
